<?php 
$found = false;
$buscar = false;
$oper = '';
			$codart = '';
			$nom = '';
			$desc ='';
			$marca = '';
			$familia = '';
			$color = '';
			$clase = '';
			$sts = '';
			$prov = '';
			$new = '';		
			$precio = '';
			$costo = '';
			$creac = '';
			$user = '';
			
			$dctoIn = 0;
			$dctoOut = 0;
		
$codart = '';
include('../Connections/KRAPPERCON.php')  ;
include ("../Connections/Accesos.php");

if (isset($_GET['oper'])){
$oper = $_GET['oper'];
}

if ($oper == 'view'){
	$buscar  = true;
	$codart = getGet('codart');
	$sql = "call sp_getArticulo('".$codart."')";
	$res = $mysqli->query($sql);
	
	if ($res){
		if ($row = $res->fetch_assoc()){
			$found = true;
			$codart = $row['ArticuloID'];
			$nom = $row['ArticuloName'];
			$desc = $row['ArticuloDesc'];
			$marca = $row['MarcaID'];
			$familia = $row['FamilyID'];
			$color = $row['ColorID'];
			$clase = $row['ClaseID'];
			$sts = $row['ArticuloSts'];
			$prov = $row['ProveedID'];
			$new = $row['ArticuloIDNew'];			
			$precio = $row['ArticuloPre'];
			$costo = $row['ArticuloCos'];
			$creac = $row['ArticuloFeCre'];
			$user = $row['UserID'];
			
			$dctoIn = $row['ArticuloDsc1'];
			$dctoOut = $row['ArticuloDsc2'];
		}
		$res->close();
		$mysqli->check_result();
	}
}
?>
<div>

<input type="button"  name="btn_nart" id="btn_nart" onclick="nuevoArt()" value="Nuevo" />

    <input name="btn_grabar" type="button" id="btn_grabar" onclick="grabar()" value="Grabar" />
    <input name="btn_Listado" type="button" id="btn_Listado" onclick="busqueda()" value="Listado" />
    &nbsp;&nbsp;<input type="text" id="t_ma_codart" value="" onchange="searchCodigo()" />
</div>
<table width="100%" border="0" id="tblart"  cellpadding="0" cellspacing="0">
    <?php 
	if ($found){
	?>
  <tr>
    <td colspan="6">
    <div class="subTit">
	<?php 
    echo "Articulo ".$codart." creador por ".$user." el ".$creac;
    ?>
    </div>    </td>
  </tr>
  <?php 
	
	}
	?>
  <tr>
    <td>Codigo</td>
    <td colspan="5"><input name="t_cod" type="text" class="clsReq" id="t_cod" maxlength="15"  >
&nbsp;&nbsp;
<span class="onlyInput">
      <input type="checkbox" name="chk_auto" id="chk_auto" onchange="GEBI('t_cod').disabled = this.checked; if(this.checked) GEBI('t_cod').value='';" />&nbsp;Autogenerar      </span>   </td>
  </tr>
  <tr>
    <td >
      Articulo</td>
    <td >
      <input name="t_nomart" class="clsReq" type="text" id="t_nomart" size="40" maxlength="40"   />    </td>
    <td >Precio Venta(inc.IGV)</td>
    <td >
      <input name="t_precio" type="text"  id="t_precio" onchange="mr(this)" class="qt clsReq" size="15" maxlength="10" >
    </td>
    <td class="noView" >Costo</td>
    <td class="noView" >
      <input name="t_costo" type="text" id="t_costo" onchange="mr(this)" size="15" class="qt" maxlength="10"  >
    </td>
  </tr>
  <tr class="noView">
    <td>[%]Dscto Compra:</td>
    <td  >
      <input name="t_ar_dsc1" type="text" id="t_ar_dsc1" size="10" class="qt" maxlength="5" onchange="mr(this)" />
    </td>
    <td  >[%]Dscto Venta :</td>
    <td  ><input name="t_ar_dsc2" type="text" id="t_ar_dsc2" size="10" class="qt" maxlength="5" onchange="mr(this)" /></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr  >
  <tr>
    <td class="noView">Descripcion</td>
    <td  class="noView" colspan="3"  >
      <input name="t_descripcion"  type="text" id="t_descripcion" size="50" maxlength="45"   />      </td>
    <td>Clase</td>
    <td>
      <select name="cb_clase" id="cb_clase">
  
<?php 
include ('../Query_Min/getClasesArt.php');
?>
      </select>
    </td>
  </tr  >


  <tr>
    <td>Proveedor</td>
    <td>
      <select name="cb_proveedor" id="cb_proveedor" >
     
        <?php
			$result = "SELECT * FROM tt_proveedor";
			$res = $mysqli->query($result);
			if ($res) {
			while($row = $res->fetch_assoc()) {
			?>
			<option value= <?php echo($row["ProveedID"]) ?>> <?php echo ($row["ProveedNom"]) ?></option>
			<?php
			}
			$res->close();
			$mysqli->check_result();
			}  
?>
      </select>
    </td>
    <td>Estado</td>
    <td>
      <select name="cb_est" id="cb_est" >
        <option value="1"  selected="selected">ACTIVO</option>
        <option value="0" >INACTIVO</option>
      </select>
    </td>
    <td>Familia</td>
    <td>
      <select name="cb_familia" id="cb_familia" >      
      <?php include ('../Query_Min/getFamilias.php');
	  ?>
      </select>
    </td>
  </tr>
  <tr>
    <td>Codigo Nuevo</td>
    <td>
      <input name="t_codnew" type="text" id="t_codnew" size="25" maxlength="20" />
    </td>
    <td>Color</td>
    <td><select name="cb_color" id="cb_color" >
        <?php
		$result ="SELECT * FROM tt_color   ";
		$res = $mysqli->query($result);
		if ($res) {
		while($row = $res->fetch_assoc()) {
		?>
		<option value="<?php echo ( $row["ColorID"]) ?>" > <?php echo ($row["ColorTxt"]) ?> </option>
		<?php 		
		}
		$res->close();
		
		$mysqli->check_result();
}

?>
    </select></td>
    <td>Marca</td>
    <td><select name="cb_marca" id="cb_marca" s>
	<?php include('../Query_Min/getMarcas.php'); ?>
    </select></td>
  </tr>
  <tr>
    <td>Un.Medida</td>
    <td>
      <select name="cb_art_um" id="cb_art_um">
        <?php 
		$combo_um = '';
		$c_txt = 'X';
        include ('../Query_Min/getUMedidas.php');
		echo($combo_um);
        ?>
      </select>
    </td>
    <td>&nbsp;</td>
    <td></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</div>

<table id="tbl_artalm_itm" class="tableIn" width="100%" border="1" rules="all" cellpadding="2" cellspacing="1" >
  <thead><tr>
    <th>Act.</th>
    <th>ID</th><th>Almacen</th>     
    <th>Compra(%)</th>
    <th>Venta(%)</th>
    <th>Ubicacion</th>
  </tr></thead> <tbody>
  <?php 
  		$a_alm[] =  '';
		$a_ubi[] = '';
		$a_sts[] = '';
		$a_dsc1[] = '';
		$a_dsc2[] = '';
		$a_cos[] = '';
if ($found){
$query = "select AlmacenID,ArticuloID,ArticuloUbi,ArtAlmSts,ArtAlmDsc1,ArtAlmDsc2,ArtAlmCos from tt_articuloalm where ArticuloID = '".$codart."'";
doEcho($query);
$res = $mysqli->query($query);
if ($res) {
$chk = '';
		while($row = $res->fetch_assoc()) {
		$a_alm[] = $row['AlmacenID'];
		$a_ubi[] = $row['ArticuloUbi'];
		$a_sts[] = $row['ArtAlmSts'];
		$a_dsc1[] = $row['ArtAlmDsc1'];
		$a_dsc2[] = $row['ArtAlmDsc2'];
		$a_cos[] = $row['ArtAlmCos'];		
	?>
		
      <!-- 
        <tr>
        <td>
        <input type="checkbox" class='clsMAAct' <?php echo $chk; ?>  />
        </td>
        <td><?php echo $row['AlmacenID']; ?></td>
        <td><?php echo $row['AlmacenNom']; ?></td>        
        <td><input type="text" title="<?php echo $row['AlmacenID']; ?>" value="<?php echo $row['ArticuloUbi']; ?>" /></td>      
        </tr>
        -->
  <?php
 		}
$res->close();
		$mysqli->check_result();
}
}
//else {

	$sql = "select AlmacenID,AlmacenNom,'           ' AlmacenUbi,'           ' as ArticuloSts from tt_almacen where AlmacenID > 0;";
	$res = $mysqli->query($sql);
	if($res){	
		while($row = $res->fetch_assoc()){
		$ind = array_search($row['AlmacenID'],$a_alm);
		if ( $ind >= 0 ){
			$row['AlmacenUbi'] = $a_ubi[$ind];
			$row['ArticuloSts'] = $a_sts[$ind];
			$row['ArtAlmDsc1'] = $a_dsc1[$ind];
			$row['ArtAlmDsc2'] = $a_dsc2[$ind];
			$row['ArtAlmCost'] = $a_cos[$ind];						
			//continue;
		}
		?>
        <tr>
          <td>
            <input type="checkbox" <?php if ($row['ArticuloSts']==1) echo 'checked="checked"' ; ?> class='clsMAAct' />          </td>
        <td class="clsMAAlm"><?php echo $row['AlmacenID']; ?></td>
                <td><?php echo $row['AlmacenNom']; ?></td>
        <td><input class="qt clsMADsc1" onchange="mr(this)" type="text" value="<?php echo $row['ArtAlmDsc1']; ?>"  /></td>        
        <td><input class="qt clsMADsc2" type="text" onchange="mr(this)" value="<?php echo $row['ArtAlmDsc2']; ?>" /></td>

        <td><input class="clsMAUbi" type="text" title="<?php echo $row['AlmacenID']; ?>" value="<?php echo $row['AlmacenUbi']; ?>" /></td>        
        </tr>
        <?php 
		}
	$res->close();

		$mysqli->check_result();
	}

?></tbody>
</table>
<script type="text/javascript">

<?php  if (isset($_POST['tip'])&& $_POST['tip']!=""){ 
?>
$('#t_cod').attr('disabled','disabled');
$('#t_costo').attr('disabled','disabled');

<?php 
}

 ?>


function nuevoArt(){
	loadPage('Mantenimientos/MantArt.php');
}

function busqueda(){
loadPage('Consultas/LstArticulos.php',{oculcod : GEBI('t_cod').value });	
}

function nuevo(){
/* $('#tblart input').attr('disabled','disabled');*/
$('#tblart input').removeAttr('disabled').val('');
$('#tbl input').removeAttr('disabled').val('');
$('#tbl input').removeAttr('checked','false');
/*$('#t_codprv').attr('disabled','disabled');*/
<?php
/* echo "GEBI('cb_marca').value =''    ,";
echo "GEBI('cb_clase').value =''    ,";
echo "GEBI('cb_proveedor').value =''   ,"; 
echo "GEBI('cb_est').value =''         ," ;
echo "GEBI('cb_familia').value =''     ," ;
echo "GEBI('cb_color').value ='' ;   " ; */?>
}

function grabar(){

	if (!$('#tblart').checkInput()){
	return;
	}

	var art = GEBI('t_cod').value;
	var cant = 0;
	var alm= '' ;
	var cent= '' ;
	var itms = '';
	var oper= '';
	
	if ($.trim(art) == '' ){
	oper = 'add';
	}
	else {
	oper = 'upd';
	}
	var act1='';
	var res = $('#tbl_artalm_itm tbody tr').each(
	function(){
	var fila = $(this);
	var alm = fila.find('.clsMAAlm').html();
	var ubi = fila.find('.clsMAUbi').val();
	var act = fila.find('.clsMAAct').is(':checked');
	var dsc1 = pFloat(fila.find('.clsMADsc1').val());
	var dsc2 = pFloat(fila.find('.clsMADsc2').val());
	//alert(act);
	if (act == true){
	act1 = '1';
	}
	else {
	act1 = '0';
	}
	//alert(act1);
	itms = itms + alm + '|' + ubi + '|' +  dsc1 + '|' + dsc2 + '|' + act1 +  '#';
	});
	
	
	sendData('Mantenimientos/Opers/OpeArt.php',{
	oper : 'upd',
	codart : GEBI('t_cod').value ,
	nomart : GEBI('t_nomart').value ,
	txtdesc : GEBI('t_descripcion').value ,
	cbxmarca : GEBI('cb_marca').value ,
	cbxfamilia : GEBI('cb_familia').value ,
	cbxcolor : GEBI('cb_color').value ,
	cbxclase : GEBI('cb_clase').value ,
	posicion :  GEBI('t_posicion').value,
	txtprecio : GEBI('t_precio').value ,
	txtcosto : GEBI('t_costo').value ,
	cbxest : GEBI('cb_est').value,
	cbxproveedor : GEBI('cb_proveedor').value ,
	txtcodnew : GEBI('t_codnew').value,
	dsctoIn : GEBI('t_ar_dsc1').value,
	dsctoOut : GEBI('t_ar_dsc2').value,
	items : itms
	});	
}


<?php 
if ($found){
//$query = "call sp_getArticulo('".$codart."') ;";
	//$res = $mysqli->query($query);
	//if ($res) {
		//	if($row = $res->fetch_assoc()) {
				echo "GEBI('t_cod').value ='".$codart."';" ;
				echo "GEBI('t_nomart').value ='".$nom."' ;" ;
				echo "GEBI('t_precio').value ='".$precio."';" ;
				echo "GEBI('t_costo').value ='".$costo."';" ;
				echo "GEBI('t_descripcion').value ='".$desc."';" ;
				echo "GEBI('cb_clase').value ='".$clase."';";
				echo "GEBI('cb_clase').disable = true;";
				echo "GEBI('cb_proveedor').value ='".$prov."';" ;
				echo "GEBI('cb_est').value ='".$sts."';" ;
				echo "GEBI('cb_familia').value ='".$familia."';" ;
				echo "GEBI('t_codnew').value ='".$new."';" ;
				echo "GEBI('cb_marca').value ='".$marca."'; " ;
				echo "GEBI('t_ar_dsc1').value ='".number_format($dctoIn,2)."';";
				echo "GEBI('t_ar_dsc2').value ='".number_format($dctoOut,2)."';";				
	//echo "GEBI('t_posicion').value ='".$row['Posicion']."';" ;
				echo "GEBI('cb_color').value ='".$color."';" ;
				echo "$('.onlyInput').addClass('noView');";
				echo "GEBI('t_cod').disabled = true;";
			//}
//	$res->close();
	//$mysqli->check_result();
	//}


}

?>

function searchCodigo(){
var cod = GEBI('t_ma_codart').value;
loadPage('Mantenimientos/MantArt.php',{oper : 'view', codart : cod});
}

</script>


